﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class NewsManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            get_All_Category_News();
            get_All_News();
        }        
    }

    public void get_All_News()
    {
        cpPaging.PageSize = 30;
        cpPaging.DataSource = BusLogic.News.get_ALL_NEWS().Tables[0].DefaultView;
        cpPaging.BindToControl = NewsRepeater;
        NewsRepeater.DataSource = cpPaging.DataSourcePaged;
        NewsRepeater.DataBind();
    }

    protected void lbtDelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in NewsRepeater.Items)
        {
            CheckBox cb = (CheckBox)item.FindControl("cbxCheckNews");

            if (cb.Checked == true)
            {
                TextBox txtNewsId = (TextBox)item.FindControl("txtNewsId");
                BusLogic.News.remove_NEWS(int.Parse(txtNewsId.Text));
            }
        }
        get_All_News();
        cbxCheckALLNews.Checked = false;
    }

    protected void cbxCheckALLNews_CheckedChanged(object sender, EventArgs e)
    {
        if (!cbxCheckALLNews.Checked)
        {
            foreach (RepeaterItem item in NewsRepeater.Items)
            {
                CheckBox cb = (CheckBox)item.FindControl("cbxCheckNews");
                cb.Checked = false;
            }
        }
        else
        {
            foreach (RepeaterItem item in NewsRepeater.Items)
            {
                CheckBox cb = (CheckBox)item.FindControl("cbxCheckNews");
                cb.Checked = true;
            }
        }
    }

    public void get_All_Category_News()
    {
        ddlCategoryName.DataSource = BusLogic.Category.get_ALL_CATEGORY();
        ddlCategoryName.DataValueField = "ID";
        ddlCategoryName.DataTextField = "CATE_NAME";
        ddlCategoryName.DataBind();

        //ListItem item = new ListItem();
        //item.Value = "0";
        //item.Text = "Danh mục tin tức";
        //item.Selected = true;
        //ddlCategoryName.Items.Add(item);
        //ddlCategoryName.SelectedValue = "0";
    }

    protected void btSearch_Click(object sender, EventArgs e)
    {
        string sCatId = ddlCategoryName.SelectedValue;

        cpPaging.PageSize = 30;
        cpPaging.DataSource = BusLogic.News.search_NEWS(txtNewsName.Text, int.Parse(sCatId)).Tables[0].DefaultView;
        cpPaging.BindToControl = NewsRepeater;
        NewsRepeater.DataSource = cpPaging.DataSourcePaged;
        NewsRepeater.DataBind();

        //NewsRepeater.DataSource = BusLogic.News.search_NEWS(txtNewsName.Text, int.Parse(sCatId));
        //NewsRepeater.DataBind();
    }

    protected void btReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("NewsManager.aspx");
    }
}
